package sy.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import sy.pageModel.*;
import sy.service.StorerServiceI;
import sy.util.ConfigUtil;

/**
 * storer控制器
 * 
 * 
 */
@Controller
@RequestMapping("/storerController")
public class StorerController extends BaseController {

	@Autowired
	private StorerServiceI storerService;


	/**
	 * 跳转到storer管理页面
	 * 
	 * @return
	 */
	@RequestMapping("/manager")
	public String manager() {
		return "/business/storer";
	}
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(Storer storer, PageHelper ph) {
		return storerService.dataGrid(storer, ph);
	}

	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		Storer storer = new Storer();
		storer.setId(UUID.randomUUID().toString());
		request.setAttribute("storer", storer);
		return "/business/storerAdd";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(Storer storer) {
		Json j = new Json();
		try {
			storerService.add(storer);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(storer);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, String id) {
		Storer storer = storerService.get(id);
		request.setAttribute("storer", storer);
		return "/business/storerEdit";
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Storer storer) {
		Json j = new Json();
		try {
			storerService.edit(storer);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setObj(storer);
		} catch (Exception e) {
			// e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(String id, HttpSession session) {
		Json j = new Json();
		try{
			storerService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		}catch (Exception e){
			j.setMsg("删除失败！");
			j.setSuccess(false);
		}
		return j;
	}

	@RequestMapping("/batchDelete")
	@ResponseBody
	public Json batchDelete(String ids, HttpSession session) {
		Json j = new Json();
		if (ids != null && ids.length() > 0) {
			for (String id : ids.split(",")) {
				if (id != null) {
					this.delete(id, session);
				}
			}
		}
		j.setMsg("批量删除成功！");
		j.setSuccess(true);
		return j;
	}
    //导入
	@RequestMapping("/storerImport")
	@ResponseBody
	synchronized public Json saveproductwithExcelImport(@RequestParam("file") MultipartFile file){  
		Json j = new Json();
		String message=	storerService.savestorerwithExcel(file);
		j.setSuccess(true);
		j.setMsg(message);
		return j;
   }

	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree(HttpSession session) {
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		return storerService.tree(sessionInfo);
	}
}
